/**
 * 
 */
package com.nari.interfaces.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.nari.heatshare.model.IAssignMeterDataGather;
import com.nari.heatshare.service.IAssignMeterDataStatisticsService;
import com.nari.heatshare.service.ITempPointDataStatisticsService;
import com.nari.interfaces.dao.IAssignMeterDataBufferDao;
import com.nari.interfaces.dao.IHeatMeterDataBufferDao;
import com.nari.interfaces.dao.IMissionFailureLogDao;
import com.nari.interfaces.dao.IUploadMissionStateDao;
import com.nari.interfaces.decipher.gprs.vo.ClientHost;
import com.nari.interfaces.decipher.gprs.vo.UploadMeterDataSeal;
import com.nari.interfaces.model.IAssignMeterDataBuffer;
import com.nari.interfaces.model.IHeatMeterData24Gather;
import com.nari.interfaces.model.IHeatMeterDataBuffer;
import com.nari.interfaces.model.IMissionFailureLog;
import com.nari.interfaces.model.IUploadMissionState;
import com.nari.interfaces.utils.MeterData2BufferUtil;
import com.nari.interfaces.utils.entityconvert.AssignMeterData2Buffer;
import com.nari.interfaces.utils.entityconvert.Buffer2AssignMeterData;
import com.nari.interfaces.utils.entityconvert.Buffer2HeatMeterData;
import com.nari.interfaces.utils.entityconvert.HeatMeterData2Buffer;
import com.nari.platform.date.CalendarUtils;

/**
 * 
 * @author Xiaopeng
 * @date 2013-11-4
 */
public class InterfacesHandlerServiceImpl implements IInterfacesHandlerService {
	private IHeatMeterData24GatherService iHeatMeterData24GatherService;
	private IAssignMeterDataBufferDao iAssignMeterDataBufferDao;
	private IHeatMeterDataBufferDao iHeatMeterDataBufferDao;
	private IUploadMissionStateDao iUploadMissionStateDao;
	private IAssignMeterDataStatisticsService iAssignMeterDataStatisticsService;
	private ITempPointDataStatisticsService iTempPointDataStatisticsService;
	private IMissionFailureLogDao iMissionFailureLogDao;
	
	/**
	 * 记录数据抄收异常日志
	 * @param frameDecoderLogList
	 * @param clientHost
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-2-6
	 */
	public void saveMissionExceptionLog(List<IMissionFailureLog> frameDecoderLogList, 
			ClientHost clientHost) throws Exception {
		if(frameDecoderLogList!=null && frameDecoderLogList.size()>0) {
			for(IMissionFailureLog exceptionLog: frameDecoderLogList) {
				if(clientHost != null) {
					exceptionLog.setClientHost(clientHost.toString());
				}
				iMissionFailureLogDao.saveEntity(exceptionLog);
			}
		}
	}
	
	/**
	 * 记录系统异常日志
	 * @param systemLog
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-6-19
	 */
	public void saveSystemExceptionLog(IMissionFailureLog systemLog) throws Exception {
		iMissionFailureLogDao.saveEntity(systemLog);
	}
	
	/**
	 * 保存接口上传的数据列表
	 * @param meterDataSeal
	 * @param clientHost
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-11
	 */
	public synchronized void saveUploadMeterDatas(UploadMeterDataSeal meterDataSeal, 
			ClientHost clientHost) throws Exception {
		if(meterDataSeal != null) {
			//抄表时间
			Date recordTime = null;
			if(meterDataSeal.getRecordTime() != null) {
				recordTime = CalendarUtils.parseStringToDate(meterDataSeal.getRecordTime());
			}
			//新增数据解析异常信息
			this.saveMissionExceptionLog(meterDataSeal.getFrameDecoderLogList(), clientHost);
			//批量新增集抄的热量表数据
			iHeatMeterData24GatherService.saveMeterDataBatch(meterDataSeal.getHeatMeterDatas(), null, 2); 
			//屏蔽序列号为000、001的分配表数据
			if(meterDataSeal.getAssignMeterDatas().size() > 0) {
				for(int index=meterDataSeal.getAssignMeterDatas().size()-1; index>=0; index--) {
					IAssignMeterDataGather meterData = meterDataSeal.getAssignMeterDatas().get(index);
					if(meterData.getSeriousNum().endsWith("000") || meterData.getSeriousNum().endsWith("001")) {
						meterDataSeal.getAssignMeterDatas().remove(index);
					}
				}
			}
			//保存分配表数据
			iAssignMeterDataStatisticsService.saveMeterDataBatch(meterDataSeal.getAssignMeterDatas(), 
					meterDataSeal.getConcentratorNo(), recordTime, null, 2); //定点上传生成痕迹文件
			//上传温度点数据
			iTempPointDataStatisticsService.saveOrUpdateTempPointDataBatch(meterDataSeal.getTempPointDatas(),
					meterDataSeal.getConcentratorNo(), recordTime, 2);
		}
	}
	
	/**
	 * 缓存接口上传任务批次数据
	 * @param meterDataSeal
	 * @param clientHost
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-12
	 */
	public synchronized void saveOrUpdateBufferedMeterDataSeal(UploadMeterDataSeal meterDataSeal, 
			ClientHost clientHost) throws Exception {
		//新增数据解析异常信息
		this.saveMissionExceptionLog(meterDataSeal.getFrameDecoderLogList(), clientHost);
		//任务批次唯一标识
		String uploadMissionUID = ""; 
		//任务批次状态数据
		IUploadMissionState missionState = null;
		if(meterDataSeal.getConcentratorNo()!=null && meterDataSeal.getRecordTime()!=null) {
			missionState = iUploadMissionStateDao.queryMissionByMark(meterDataSeal.getConcentratorNo(), meterDataSeal.getRecordTime());
			if(missionState != null) { 
				//会话状态比较：若当前帧数不大于缓存的帧数，则数据不予缓存
				if(meterDataSeal.getCurrFrameIndex() <= missionState.getCurrFrameIndex()) {
					return;
				}
				uploadMissionUID = missionState.getId();
				BeanUtils.copyProperties(missionState, meterDataSeal);
				//记录任务缓存时间
				missionState.setCreateTime(new Date());
				this.iUploadMissionStateDao.updateEntity(missionState, uploadMissionUID);
			} else {
				missionState = new IUploadMissionState();
				BeanUtils.copyProperties(missionState, meterDataSeal);
				//记录任务缓存时间
				missionState.setCreateTime(new Date());
				uploadMissionUID = this.iUploadMissionStateDao.saveEntity(missionState);
			}
			//分配表数据缓存
			List<IAssignMeterDataBuffer> assignMeterDataBuffer = MeterData2BufferUtil.assignMeterData2buffer(meterDataSeal.getAssignMeterDatas(), 
					new AssignMeterData2Buffer() {
				public IAssignMeterDataBuffer convert(IAssignMeterDataGather meterData) throws Exception {
					IAssignMeterDataBuffer bufferData = new IAssignMeterDataBuffer();
					BeanUtils.copyProperties(bufferData, meterData);
					return bufferData;
				}
			});
			this.iAssignMeterDataBufferDao.saveMeterDataBatch(uploadMissionUID, assignMeterDataBuffer);
			//热量表数据缓存
			List<IHeatMeterDataBuffer> heatMeterDataBuffer = MeterData2BufferUtil.heatMeterData2Buffer(meterDataSeal.getHeatMeterDatas(), 
					new HeatMeterData2Buffer() {
				public IHeatMeterDataBuffer convert(IHeatMeterData24Gather meterData) throws Exception {
					IHeatMeterDataBuffer bufferData = new IHeatMeterDataBuffer();
					BeanUtils.copyProperties(bufferData, meterData);
					return bufferData;
				}
			});
			this.iHeatMeterDataBufferDao.saveMeterDataBatch(uploadMissionUID, heatMeterDataBuffer);
		}
	}
	
	/**
	 * 查询接口上传任务批次数据
	 * @param concentratorNo
	 * @param recordTime
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-12
	 */
	public synchronized UploadMeterDataSeal queryBufferedMeterDataSeal(String concentratorNo, 
			String recordTime) throws Exception {
		//任务批次状态数据
		IUploadMissionState missionState = iUploadMissionStateDao.queryMissionByMark(concentratorNo, recordTime);
		if(missionState != null) {
			UploadMeterDataSeal meterDataSeal = new UploadMeterDataSeal();
			BeanUtils.copyProperties(meterDataSeal, missionState);
			//分配表数据
			List<IAssignMeterDataBuffer> assignMeterDataBuffer = iAssignMeterDataBufferDao.queryMeterDataListByMissionUID(missionState.getId());
			List<IAssignMeterDataGather> assignMeterDatas = MeterData2BufferUtil.buffer2AssignMeterData(assignMeterDataBuffer, new Buffer2AssignMeterData() {
				public IAssignMeterDataGather convert(IAssignMeterDataBuffer bufferData) throws Exception {
					IAssignMeterDataGather meterData = new IAssignMeterDataGather();
					BeanUtils.copyProperties(meterData, bufferData);
					meterData.setId(null);
					return meterData;
				}
			});
			if(assignMeterDatas!=null && assignMeterDatas.size()>0) {
				meterDataSeal.getAssignMeterDatas().addAll(assignMeterDatas);
			}
			//热量表数据
			List<IHeatMeterDataBuffer> heatMeterDataBuffer = iHeatMeterDataBufferDao.queryMeterDataListByMissionUID(missionState.getId());
			List<IHeatMeterData24Gather> heatMeterDatas = MeterData2BufferUtil.buffer2HeatMeterData(heatMeterDataBuffer, new Buffer2HeatMeterData() {
				public IHeatMeterData24Gather convert(IHeatMeterDataBuffer bufferData) throws Exception {
					IHeatMeterData24Gather meterData = new IHeatMeterData24Gather();
					BeanUtils.copyProperties(meterData, bufferData);
					meterData.setId(null);
					return meterData;
				}
			});
			if(heatMeterDatas!=null && heatMeterDatas.size()>0) {
				meterDataSeal.getHeatMeterDatas().addAll(heatMeterDatas);
			}
			return meterDataSeal;
		}
		return null;
	}
	
	/**
	 * 删除接口上传任务批次数据
	 * @param uploadMissionState
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-11-12
	 */
	public synchronized void deleteMissionStateDataByMark(String concentratorNo, String recordTime) throws Exception {
		//任务批次状态数据
		IUploadMissionState missionState = iUploadMissionStateDao.queryMissionByMark(concentratorNo, recordTime);
		if(missionState != null) {
			//删除缓存的分配表数据
			this.iAssignMeterDataBufferDao.deleteMeterDataByMissionUID(missionState.getId());
			//删除缓存的热量表数据
			this.iHeatMeterDataBufferDao.deleteMeterDataByMissionUID(missionState.getId());
		}
		//删除任务批次状态数据
		this.iUploadMissionStateDao.deleteMissionByMark(concentratorNo, recordTime);
	}

	/**
	 * @return the iHeatMeterData24GatherService
	 */
	public IHeatMeterData24GatherService getiHeatMeterData24GatherService() {
		return iHeatMeterData24GatherService;
	}

	/**
	 * @param iHeatMeterData24GatherService the iHeatMeterData24GatherService to set
	 */
	public void setiHeatMeterData24GatherService(
			IHeatMeterData24GatherService iHeatMeterData24GatherService) {
		this.iHeatMeterData24GatherService = iHeatMeterData24GatherService;
	}

	/**
	 * @return the iAssignMeterDataBufferDao
	 */
	public IAssignMeterDataBufferDao getiAssignMeterDataBufferDao() {
		return iAssignMeterDataBufferDao;
	}

	/**
	 * @param iAssignMeterDataBufferDao the iAssignMeterDataBufferDao to set
	 */
	public void setiAssignMeterDataBufferDao(
			IAssignMeterDataBufferDao iAssignMeterDataBufferDao) {
		this.iAssignMeterDataBufferDao = iAssignMeterDataBufferDao;
	}

	/**
	 * @return the iHeatMeterDataBufferDao
	 */
	public IHeatMeterDataBufferDao getiHeatMeterDataBufferDao() {
		return iHeatMeterDataBufferDao;
	}

	/**
	 * @param iHeatMeterDataBufferDao the iHeatMeterDataBufferDao to set
	 */
	public void setiHeatMeterDataBufferDao(
			IHeatMeterDataBufferDao iHeatMeterDataBufferDao) {
		this.iHeatMeterDataBufferDao = iHeatMeterDataBufferDao;
	}

	/**
	 * @return the iUploadMissionStateDao
	 */
	public IUploadMissionStateDao getiUploadMissionStateDao() {
		return iUploadMissionStateDao;
	}

	/**
	 * @param iUploadMissionStateDao the iUploadMissionStateDao to set
	 */
	public void setiUploadMissionStateDao(
			IUploadMissionStateDao iUploadMissionStateDao) {
		this.iUploadMissionStateDao = iUploadMissionStateDao;
	}

	/**
	 * @return the iAssignMeterDataStatisticsService
	 */
	public IAssignMeterDataStatisticsService getiAssignMeterDataStatisticsService() {
		return iAssignMeterDataStatisticsService;
	}

	/**
	 * @param iAssignMeterDataStatisticsService the iAssignMeterDataStatisticsService to set
	 */
	public void setiAssignMeterDataStatisticsService(
			IAssignMeterDataStatisticsService iAssignMeterDataStatisticsService) {
		this.iAssignMeterDataStatisticsService = iAssignMeterDataStatisticsService;
	}

	/**
	 * @return the iTempPointDataStatisticsService
	 */
	public ITempPointDataStatisticsService getiTempPointDataStatisticsService() {
		return iTempPointDataStatisticsService;
	}

	/**
	 * @param iTempPointDataStatisticsService the iTempPointDataStatisticsService to set
	 */
	public void setiTempPointDataStatisticsService(
			ITempPointDataStatisticsService iTempPointDataStatisticsService) {
		this.iTempPointDataStatisticsService = iTempPointDataStatisticsService;
	}

	/**
	 * @return the iMissionFailureLogDao
	 */
	public IMissionFailureLogDao getiMissionFailureLogDao() {
		return iMissionFailureLogDao;
	}

	/**
	 * @param iMissionFailureLogDao the iMissionFailureLogDao to set
	 */
	public void setiMissionFailureLogDao(IMissionFailureLogDao iMissionFailureLogDao) {
		this.iMissionFailureLogDao = iMissionFailureLogDao;
	}

}
